package com.happyma.bbn.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;

import androidx.preference.PreferenceManager;

import java.util.Locale;

public class Common {


    private static final String TAG = "Common-->";

    public static  boolean isNetworkAvailable(){

        ConnectivityManager connManager = (ConnectivityManager) ContextGeeter.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= 23) {
            //获取网络属性
            NetworkCapabilities networkCapabilities = connManager.getNetworkCapabilities(connManager.getActiveNetwork());
            if (networkCapabilities != null) {
                return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
            }
        } else {
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo != null) {
                return networkInfo.isConnected();
            }
        }
        return false;
    }

    public static String getCurrentLanguage()
    {
        String str1 = "English";
        String str2 = Locale.getDefault().getLanguage();
        if (str2.equals(new Locale("en_us").getLanguage())) {
            str1 = "English";
        } else if (str2.equals(new Locale("es").getLanguage())) {
            str1 = "Español";
        } else if (str2.equals(new Locale("pt_br").getLanguage())) {
            str1 = "Português";
        } else if (str2.equals(new Locale("de").getLanguage())) {
            str1 = "Deutsch";
        } else if (str2.equals(new Locale("ru").getLanguage())) {
            str1 = "Pусский";
        } else if (str2.equals(new Locale("zh_cn").getLanguage())) {
            str1 = "简体中文";
        } else if (str2.equals(new Locale("zh_tw").getLanguage())) {
            str1 = "繁體中文";
        } else if (str2.equals(new Locale("ja").getLanguage())) {
            str1 = "日本語";
        } else if (str2.equals(new Locale("ko").getLanguage())) {
            str1 = "한국어";
        }
        return str1;
    }

    public static String getPreferredLanguageString(){
        String defLanguage = "ChineseSimplified";
        String localeStr = new Locale(PreferenceManager.getDefaultSharedPreferences(ContextGeeter.getAppContext()).getString("lang_setting","")).getLanguage();
        if (localeStr.equals(new Locale("en_us").getLanguage())){
            defLanguage = "English";
        } else if (localeStr.equals(new Locale("es").getLanguage())) {
            defLanguage = "Spanish";
        } else if (localeStr.equals(new Locale("pt_br").getLanguage())) {
            defLanguage = "Portuguese";
        } else if (localeStr.equals(new Locale("de").getLanguage())) {
            defLanguage = "German";
        } else if (localeStr.equals(new Locale("ru").getLanguage())) {
            defLanguage = "Russian";
        } else if (localeStr.equals(new Locale("zh_cn").getLanguage())) {
            defLanguage = "ChineseSimplified";
        } else if (localeStr.equals(new Locale("zh_tw").getLanguage())) {
            defLanguage = "ChineseTraditional";
        } else if (localeStr.equals(new Locale("ja").getLanguage())) {
            defLanguage = "Japanese";
        } else if (localeStr.equals(new Locale("ko").getLanguage())) {
            defLanguage = "Korean";
        }
        return defLanguage;
    }
}
